import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

// 导入登录,注册页面组件
// import login from '@/views/login/login.vue'
const login = () => import('@/views/login/login.vue')
// import reg from '@/views/reg/reg.vue'
const reg = () => import('@/views/reg/reg.vue')
// import Main from '@/views/main/main.vue'
const Main = () => import('@/views/main/main.vue')

// 二级路由
// import Home from '@/views/home/home.vue'
const Home = () => import('@/views/home/home.vue')
// import useinfo from '@/views/Menus/User/userinfo.vue'
const useinfo = () => import('@/views/Menus/User/userinfo.vue')
// import headinfo from '@/views/Menus/head/headinfo.vue'
const headinfo = () => import('@/views/Menus/head/headinfo.vue')
// import userpwd from '@/views/Menus/pwd/user-pwd.vue'
const userpwd = () => import('@/views/Menus/pwd/user-pwd.vue')
// import artcart from '@/views/Menus/cate/art-cate.vue'
const artcart = () => import('@/views/Menus/cate/art-cate.vue')
// import artlist from '@/views/Menus/art_list/art-list.vue'
const artlist = () => import('@/views/Menus/art_list/art-list.vue')
Vue.use(VueRouter)

const routes = [
  // 添加路由规则
  { path: '/login', component: login, name: 'login' },
  { path: '/reg', component: reg },
  {
    path: '/',
    component: Main,
    redirect: '/home',
    children: [
      { path: '/home', component: Home },
      { path: 'user-info', component: useinfo },
      { path: 'user-avatar', component: headinfo },
      { path: 'user-pwd', component: userpwd },
      { path: 'art-cate', component: artcart },
      { path: 'art-list', component: artlist }
    ]
  }

]

const router = new VueRouter({
  routes
})

// 设置全局守卫 判断有无token值
router.beforeEach((to, from, next) => {
  if (to.path === '/login' || to.path === '/reg') {
    next()
  } else {
    if (store.state.uers.token) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
